<?php

class Comment extends CActiveRecord {
    const TYPE_SERVICE = 1;
    const TYPE_SUGGEST = 2;
    const TYPE_HELP = 1;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'Comment';
    }

    public function relations() {
        return array(
            'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
            'rt' => array(self::HAS_MANY, 'Rating', 'item_id', 'together' => false, 'condition' => 'rt.user_id = ' . Y::userId() . ' AND rt.type=' . Rating::TYPE_COMMENT),);
    }

    public function rules() {
        return array(
            array('text', 'required')
        );
    }

    public function attributeLabels() {
        return array(
            'text' => 'Комментарий',
            't_create' => 'Время создания',
            'owner_id' => 'id Создателя',
            'post_id' => 'Пост'
        );
    }

    public function beforeSave() {
        if(parent::beforeSave()) {
            if($this->isNewRecord) {
                $this->t_create = time();
                $this->owner_id = Yii::app()->user->id;

            }
            return true;
        }
        return false;
    }

    public function beforeDelete() {
        // delete all helps
        $criteria = new CDbCriteria();
        $criteria->condition = "comment_id = :id";
        $criteria->params = array(":id" => $this->id);

    }

}
